package com.zyq.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.zyq.mapper.SearchWordMapper;
import com.zyq.model.entity.Topic;
import com.zyq.model.request.ContentDto;
import com.zyq.model.response.CompleteTopicResponse;
import com.zyq.model.response.HomePage10Response;
import com.zyq.util.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zyq.mapper.TopicMapper;
import com.zyq.service.ITopicService;

/**
 * 帖子Service业务层处理
 *
 * @author zyq
 * @date 2021-12-08
 */
@Service
public class TopicServiceImpl implements ITopicService
{
    @Autowired
    private TopicMapper topicMapper;
    @Autowired
    private UserUtil userUtil;
    @Autowired
    private SearchWordMapper searchWordMapper;

    /**
     * 查询帖子
     *
     * @param id 帖子ID
     * @return 帖子
     */
    @Override
    public CompleteTopicResponse selectTopicById(Long id)
    {
        CompleteTopicResponse completeTopicResponse = new CompleteTopicResponse();
        completeTopicResponse.setTopic(topicMapper.selectTopicByIdDetail(id));
        List<Topic> topics = topicMapper.selectTopicFatherId(id);
        List<CompleteTopicResponse> completeTopicResponseChildren =  new ArrayList<>(topics.size());
        for(int index  = 0 ; index < topics.size() ; index ++ ){

            CompleteTopicResponse completeTopicResponseTemp = selectTopicById(topics.get(index).getId());

            completeTopicResponseChildren.add(completeTopicResponseTemp);
        }
        completeTopicResponse.setChild(completeTopicResponseChildren);
        return completeTopicResponse;
    }

    /**
     * 查询帖子列表
     *
     * @param topic 帖子
     * @return 帖子
     */
    @Override
    public List<Topic> selectTopicList(ContentDto topic)
    {
        if(topic.getContent()!=null && topic.getContent().trim()!="" ) {
            if (searchWordMapper.ifWord(topic.getContent()) > 0) {
                searchWordMapper.updateCount(topic.getContent());
            } else
                searchWordMapper.insertWord(topic.getContent());
        }
        return topicMapper.selectTopicList(topic);
    }

    /**
     * 新增帖子
     *
     * @param topic 帖子
     * @return 结果
     */
    @Override
    public int insertTopic(Topic topic)
    {
        topic.setUserId(userUtil.getUserId());
        if(topic.getFatherId() != null && topic.getFatherId() != 0 ){
            topicMapper.updateTopicCount(topic.getFatherId());
        }
        return topicMapper.insertTopic(topic);
    }

    /**
     * 修改帖子
     *
     * @param topic 帖子
     * @return 结果
     */
    @Override
    public int updateTopic(Topic topic)
    {
        return topicMapper.updateTopic(topic);
    }

    /**
     * 批量删除帖子
     *
     * @param ids 需要删除的帖子ID
     * @return 结果
     */
    @Override
    public int deleteTopicByIds(Long[] ids)
    {
        return topicMapper.deleteTopicByIds(ids);
    }

    /**
     * 删除帖子信息
     *
     * @param id 帖子ID
     * @return 结果
     */
    @Override
    public int deleteTopicById(Long id)
    {
        return topicMapper.deleteTopicById(id);
    }

    /**
     * 通过用户查询自己发布的帖子
     * @param userId
     * @return
     */
    @Override
    public List<Topic> selectTopicListByMySelf(long userId){
        return topicMapper.selectTopicListByUserId(userId);
    }

    /**
     * 获取最新10个帖子
     * @return
     */
    @Override
    public List<HomePage10Response> select10(){
        return topicMapper.select10();
    }

    @Override
    public  List<Topic> selectTopicListByAdmin(Topic topic){
        return topicMapper.selectTopicListByAdmin(topic);
    }
}
